home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / lib / hosts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-20  |  1.6 KB  |  82 lines

  1. /*
  2.      net logger - network traffic logging library
  3.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  4.  
  5.      Please see the file `COPYING' for the complete copyright notice.
  6.  
  7. hosts.c - 03/20/93
  8.  
  9. */
  10. #include <sys/types.h>
  11. #include <netinet/in.h>
  12. #include <sys/socket.h>
  13. #include <netdb.h>
  14. #include <signal.h>
  15.  
  16. extern char *inet_ntoa(struct in_addr);
  17.  
  18. struct hncache {
  19.      struct in_addr ha;
  20.      char *hn;
  21. };
  22.  
  23. struct hncache hostcache[2048];
  24. static int hnc_init = 0;
  25. static unsigned long hit = 0, miss = 0, coll = 0;
  26.  
  27. sigusr1()
  28. {
  29.      printf("Hit/Miss: %lu/%lu, Collisions: %lu\n",
  30.         hit, miss,coll);
  31. }
  32.  
  33. char *
  34. cgethostbyaddr(struct in_addr ipha)
  35. {
  36.      unsigned int hash;
  37.      struct hostent *he;
  38.      unsigned long ha;
  39.      char *name;
  40.  
  41.      ha = htonl(ipha.s_addr);
  42.      hash = ha & 0x7ff;
  43.  
  44.      if(!hnc_init){
  45.       int i;
  46.       for(i=0;i<2048;i++){
  47.            hostcache[i].hn = (char *)0;
  48.            memset(&hostcache[i].ha, 0, 4);
  49.       }
  50.       hnc_init = 1;
  51.       signal(SIGUSR1, sigusr1);
  52.      }
  53.      
  54.      if((memcmp(&hostcache[hash].ha, &ha, 4) == 0) &&
  55.     hostcache[hash].hn){
  56.       name = hostcache[hash].hn;
  57.       hit++;
  58.      }
  59.      else {
  60.       miss++;
  61.       if(he = gethostbyaddr((char *)&ha, 4, AF_INET))
  62.            name = he->h_name;
  63.       else
  64.            name =  inet_ntoa(ipha);
  65.  
  66.       memcpy(&hostcache[hash].ha, &ha, 4);
  67.       if(hostcache[hash].hn){
  68.            free(hostcache[hash].hn);
  69.            coll++;
  70.       }
  71.       hostcache[hash].hn = (char *)malloc(strlen(name)+1);
  72.       strcpy(hostcache[hash].hn, name);
  73.      }
  74.      return name;
  75. }
  76.  
  77. void
  78. outhost(struct in_addr ha)
  79. {
  80.      printf(" %s", cgethostbyaddr(ha));
  81. }
  82.